package com.rsscollector.controller.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.rsscollector.util.PersistenceUtil;

public class EntityManagerRequestFilter implements Filter {
		
    public void doFilter(ServletRequest request,
                         ServletResponse response,
                         FilterChain chain)
            throws IOException, ServletException {

        try {
            PersistenceUtil.begin();

            // Call the next filter (continue request processing)
            chain.doFilter(request, response);

            // Commit
            PersistenceUtil.end();

        } catch (Throwable ex) {
        	PersistenceUtil.endWithError();
            throw new ServletException(ex);
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
    }

    public void destroy() {
    }
}
